package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private DishService dishService;

    /**
     * 保存套餐信息
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        //调用业务层操作
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 分页查询
     */
    @GetMapping("page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> pageInfo = setmealService.pageSetmealDto(page, pageSize, name);
        return R.success(pageInfo);
    }

    /**
     * 启售、停售状态
     */
    @PostMapping("status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, Long[] ids) {
        //update setmeal set status=? where id in ( )
        setmealService.lambdaUpdate()
                .set(Setmeal::getStatus, status)
                .in(Setmeal::getId, Arrays.asList(ids))
                .update(new Setmeal()); //update方法参数未空，自动填充失效
        return R.success("修改成功");
    }

    /**
     * 批量删除
     */
    @DeleteMapping
    public R<String> deleteByIds(Long[] ids) {
        setmealService.deleteByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 根据id查询详情
     */
    @GetMapping("{id}")
    public R<SetmealDto> getSetmealWithDish(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.getSetmealWithDish(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐信息
     */
    @PutMapping
    public R<String> updateWithDish(@RequestBody SetmealDto setmealDto) {
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }

    /**
     * 根据分类查询套餐列表
     * condition属性：表示条件成立时，将结果存入缓存中。condition属性中是不能使用result结果
     * unless属性：与condition恰好相反
     */
    @Cacheable(cacheNames = "setmeal", key = "#categoryId + '_' + #status", unless = "#result.data.size()==0")
    @GetMapping("list")
    public R<List<Setmeal>> list(Long categoryId, Integer status) {
        //select * from setmeal where category_id=? and status=?
        List<Setmeal> list = setmealService.lambdaQuery()
                .eq(categoryId != null, Setmeal::getCategoryId, categoryId)
                .eq(status != null, Setmeal::getStatus, status)
                .orderByDesc(Setmeal::getUpdateTime) //排序
                .list();
        return R.success(list);
    }

    /**
     * 查询套餐对应的菜品信息
     */
    @GetMapping("dish/{setmealId}")
    public R<List<DishDto>> setmealDish(@PathVariable Long setmealId) {
        List<DishDto> list = dishService.setmealDishDto(setmealId);
        return R.success(list);
    }
}
